package com.qf.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.dao.UserDao;
import com.qf.pojo.User;
import com.qf.service.UserService;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

/**
 * @author lixu
 */
@Service
//@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    public static final String index = "user";

    @Autowired
    private ObjectMapper objectMapper;


    @Override
    @Transactional
    public void add(User user) throws Exception {
        //一、添加Mysql
        userDao.add(user);
        //二、
        //1:创建索引库
        IndicesClient indices = restHighLevelClient.indices();
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);

        String source1 = "{\n" +
                "    \"index\": {\n" +
                "      \"number_of_shards\": \"5\",\n" +
                "      \"number_of_replicas\": \"1\"\n" +
                "    }\n" +
                "  }";

        createIndexRequest.settings(source1, XContentType.JSON);
        //Mappings
        String source2 = "{\n" +
                "      \"properties\": {\n" +
                "        \"registTime\": {\n" +
                "          \"type\": \"date\",\n" +
                "          \"format\": \"epoch_millis||yyyy-MM-dd||yyyy-MM-dd HH:mm:ss\"\n" +
                "        },\n" +
                "        \"birthday\": {\n" +
                "          \"type\": \"date\",\n" +
                "          \"format\": \"epoch_millis||yyyy-MM-dd||yyyy-MM-dd HH:mm:ss\"\n" +
                "        },\n" +
                "\t\t\"password\": {\n" +
                "          \"type\": \"keyword\"\n" +
                "        },\n" +
                "\t\t\t\t\"sex\": {\n" +
                "          \"type\": \"keyword\"\n" +
                "        },\n" +
                "        \"name\": {\n" +
                "          \"type\": \"text\",\n" +
                "          \"analyzer\": \"ik_max_word\"\n" +
                "        }\n" +
                "      }\n" +
                "\t  }";
        createIndexRequest.mapping(source2, XContentType.JSON);
        CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT);
        if (createIndexResponse.isAcknowledged()) {
            //2:保存文档
            IndexRequest indexRequest = new IndexRequest(index);
            indexRequest.id(String.valueOf(user.getId()));
            String source3 = objectMapper.writeValueAsString(user);
            indexRequest.source(source3, XContentType.JSON);
            restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        }

    }
}
